import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import BindingMobile from "../views/BindingMobile";
import RealNameOuth from "../views/RealNameOuth";
import OuthInfo from "../views/OuthInfo";
import RobOrder from "../views/RobOrder";
import OrderInfo from '../views/orderInfo';
import ShootOrder from "../views/ShootOrder";
import ShootOrderInfo from "../views/ShootOrderInfo";
import Balance from "../views/Balance";
import GetMoney from "../views/GetMoney";
import toLogin from "@libs/login";
import $store from "../store";
import localStorage from "@utils/store/localStorage";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        meta:{
            title: "Welcome in",
            isShowBack: false,
            isShowSearch:false
        },
        component: Home,
    },
    {
        path: '/bindingMobile',
        name: 'BindingMobile',
        meta:{
            title: "绑定手机",
            isShowBack: true,
            isShowSearch:false,
            // auth:true,
        },
        component: BindingMobile,
    },
    {
        path: '/realNameOuth',
        name: 'RealNameOuth',
        meta:{
            title: "实名认证",
            isShowBack: true,
            isShowSearch:false,
            isBackGroundColor:true,
            auth:true,

        },
        component: RealNameOuth,
    },
    {
        path: '/outhInfo',
        name: 'OuthInfo',
        meta:{
            title: "认证信息",
            isShowBack: true,
            isShowSearch:false,
            auth:true,
        },
        component: OuthInfo,
    },
    {
        path: '/robOrder',
        name: 'RobOrder',
        meta:{
            title: "抢单",
            isShowBack: true,
            isShowSearch:false,
            auth:true,
        },
        component: RobOrder,
    },
    {
        path: '/orderInfo',
        name: 'OrderInfo',
        meta:{
            title: "抢单详情",
            isShowBack: true,
            isShowSearch:false,
            auth:true,
        },
        component: OrderInfo,
    },
    {
        path: '/shootOrder',
        name: 'ShootOrder',
        meta:{
            title: "拍摄订单",
            isShowBack: true,
            isShowSearch:false,
            auth:true,
        },
        component: ShootOrder,
    },
    {
        path: '/shootOrderInfo',
        name: 'ShootOrderInfo',
        meta:{
            title: "拍摄订单详情",
            isShowBack: true,
            isShowSearch:false,
            auth:true,
        },
        component: ShootOrderInfo,
    },
    {
        path: '/balance',
        name: 'Balance',
        meta:{
            title: "我的余额",
            isShowBack: true,
            isShowSearch:false,
            isBackGroundColor:true,
            auth:true,
        },
        component: Balance,
    },
    {
        path: '/getMoney',
        name: 'GetMoney',
        meta:{
            title: "提现",
            isShowBack: true,
            isShowSearch:false,
            isBackGroundColor:true,
            auth:true,
        },
        component: GetMoney,
    },
    {
        path: '/verifyCode',
        name: 'VerifyCode',
        meta:{
            title: "提现",
            isShowBack: true,
            isShowSearch:false,
            isBackGroundColor:true,
            auth:true,
        },
        component: ()=> import('@/views/VerifyCode'),
    },
    {
        path: '/getMoneyInfo',
        name: 'GetMoneyInfo',
        meta:{
            title: "提现",
            isShowBack: true,
            isShowSearch:false,
            isBackGroundColor:true,
            auth:true,
        },
        component: ()=> import('@/views/GetMoneyInfo'),
    },
    {
        path: '/balanceInfo',
        name: 'BalanceInfo',
        meta:{
            title: "余额明细",
            isShowBack: true,
            isShowSearch:false,
            isBackGroundColor:true,
            auth:true,
        },
        component: ()=> import('@/views/BalanceInfo'),
    },
    {
        path: '/getMoneyList',
        name: 'GetMoneyList',
        meta:{
            title: "提现记录",
            isShowBack: true,
            isShowSearch:false,
            isBackGroundColor:true,
            auth:true,
        },
        component: ()=> import('@/views/GetMoneyList'),
    },
    {
        path: '/wechatLogin',
        name: 'WechatLogin',
        meta:{
            isShowBack: false,
            isShowSearch:false,
            isBackGroundColor:true,
        },
        component: ()=> import('@/views/WechatLogin'),
    }
]
const router = new VueRouter({
    mode: 'history',
    base: process.env.VUE_APP_BASE_ROUTER,
    routes,
    scrollBehavior(to, from) {
        from.meta.scrollTop = window.scrollY;
        return {x: 0, y: to.meta.scrollTop || 0};
    }
});
const {back, replace} = router;

router.back = function () {
    this.isBack = true;
    back.call(router);
};
router.replace = function (...args) {
    this.isReplace = true;
    replace.call(router, ...args);
};

router.beforeEach((to, form, next) => {
    const {title, isShowBack, isShowSearch,isBackGroundColor, auth} = to.meta;
    // console.log(to.name, form.name);
    const token = localStorage.get('token')
    if (auth === true && !token) {
        if (form.name === "BindingMobile") return;
        if (form.name === 'Home' && to.name === 'BindingMobile') return toLogin(true,form.fullPath)
        return toLogin(true, to.fullPath);
    }
    // console.log(process.env.VUE_APP_NAME)
    document.title = title || process.env.VUE_APP_NAME || "智能工地";
    //判断是否显示返回按钮
    isShowBack === true ? $store.commit("SHOW_BACK") : $store.commit("HIDE_BACK");
    //判断是否切换背景色
    isBackGroundColor === true ? $store.commit("SHOW_BACKGROUNDCOLOR") : $store.commit("HIDE_BACKGROUNDCOLOR");

    //判断是否现实搜索
    isShowSearch === false ? $store.commit("HIDE_SEARCH") : $store.commit("SHOW_SEARCH");
    $store.commit('SET_TITLE',title || 'Welcome in')
    // $store.commit("BACKGROUND_COLOR", backgroundColor || "#0DCDB5");
    if (!$store.getters.userInfo.umId) {
        let userInfo = null
        // if (localStorage.get('user')) {
        userInfo = localStorage.get('user')
        $store.commit('UPDATE_USERINFO', userInfo||null)
        // }
    }
    if (auth) {
        $store.dispatch("USERINFO").then(() => {});
    }
    next();
});
export default router
